using System.Linq;
using NUnit.Framework;
using OpenWaves.ActivityFeed.RavenDB;
using OpenWaves.ActivityFeed.UnitTests;
using OpenWaves.ActivityFeed.Web;
using OpenWaves.ActivityFeed.Web.Modules;
using Raven.Client.Embedded;

namespace OpenWaves.ActivityFeed.UnitTests_RavenDB
{
    [TestFixture]
    class RavenDbUpdatesBuilderTests : UpdatesBuilderTests
    {
        protected EmbeddableDocumentStore documentStore;

        [SetUp]
        public void InitializeDocumentStore()
        {
            this.documentStore = new EmbeddableDocumentStore() { RunInMemory = true };

            DocumentStoreFactory.InitializeDocumentStore(this.documentStore);
        }

        [TearDown]
        public void DisposeDocumentStore()
        {
            this.documentStore.Dispose();
        }

        // ***** ***** ***** ***** ***** 
        // *****  
        // *****  S E A R C H
        // *****  
        // ***** ***** ***** ***** ***** 
        [Test]
        public void Given_test_updates_When_searchTerm_is_assigned_Then_GetUpdates_returns_limited_list_of_updates()
        {
            const string searchTerm = "update2";

            Given(CreateUpdates);

            this.Then(service =>
            {

                ActivityFeedBuilder activityFeedBuilder = this.CreateActivityFeedBuilder("user1111", service);
                activityFeedBuilder.PaginationRequest = new PaginationRequest() { PageSize = 5 };
                activityFeedBuilder.SearchTerms = searchTerm;
                activityFeedBuilder.UserGroups = Enumerable.Empty<UserGroup>();
                activityFeedBuilder.TopicsFilter = Enumerable.Empty<Topic>();
                PaginationStatus status;
                var updates = activityFeedBuilder.BuildActivityFeed(out status).ToList();
                Assert.That(updates.Count, Is.EqualTo(1));
                Assert.That(((Post)(updates[0].Content)).Text, Is.EqualTo("update2"));
            });
        }

        protected override IActivityFeedTestSession CreateSession()
        {
            return new RavenDbActivityFeedServiceTestSession(this.documentStore.OpenSession());
        }

        protected override ActivityFeedBuilder CreateActivityFeedBuilder(string subscriberId, IActivityFeedService activityFeedService)
        {
            return new OpenWaves.ActivityFeed.Web.RavenDB.PersonalActivityFeedBuilder(subscriberId, activityFeedService,
                                                                                      updateDtoBuilder);           
        }
    }
}